package com.example.teacher.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.Course;
import com.example.common.domain.pojo.CourseTeacher;
import com.example.common.utils.CacheClient;
import com.example.teacher.Service.CourseService;
import com.example.teacher.mapper.CourseMapper;
import com.example.teacher.mapper.CourseTeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Override
    public Course getCourseByIdWithLogicalExpire(String id) {
        // 使用逻辑过期策略获取教师信息
        return cacheClient.queryWithLogicalExpire(
                RdsConstants.COURSE_INFO,
                id,
                Course.class,
                this::getById, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                false
        );
    }

    @Override
    @Transactional
    public Course getCourseByTeacherId(String teacherId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getTeacherId, teacherId);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(queryWrapper);
        if (courseTeacher == null) {
            return null;
        }
        return getCourseByIdWithLogicalExpire(courseTeacher.getCourseId());
    }


}
